package com.umeox.phone.receiver;

import static com.umeox.phone.util.Utils.saveLog;

import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.AndroidUtils;
import com.umeox.phone.ui.InCallUIActivity;
import com.umeox.phone.util.CacheUtils;
import com.umeox.phone.util.Utils;
import com.umeox.watch.moto.dataservice.DataProvider;

public class BasePhoneStateListener extends PhoneStateListener {

    private final Context mContext;
    private long offHookTime;
    private static final String TAG = "BasePhoneStateListener";

    public BasePhoneStateListener(Context context) {
        this.mContext = context;
    }

    private static BasePhoneStateListener mBasePhoneStateListener;

    public static BasePhoneStateListener getInstance(Context context) {

        if (mBasePhoneStateListener == null) {
            synchronized (BasePhoneStateListener.class) {
                if (mBasePhoneStateListener == null) {
                    return new BasePhoneStateListener(context);
                }
            }
        }
        return mBasePhoneStateListener;
    }

    @Override
    public void onCallStateChanged(int state, String number) {
        super.onCallStateChanged(state, number);

        Log.d(TAG, "BasePhoneStateListener$onCallStateChanged state:" + state);
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                CacheUtils.setCallMonitoring(mContext, false);
                Utils.sendListenCallEndBroadCast(mContext);
                Utils.sendEndCallBroadCast(mContext, 1, offHookTime);
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                String contactName = Utils.getContactNameFromPhoneBook(mContext, number);
                boolean callMonitoring = CacheUtils.isCallMonitoring(mContext);
                boolean isCalling = CacheUtils.isCallStateCalling(mContext);
                final boolean refusePhone = DataProvider.isRefusePhone();
//                final boolean disablePhoneCall = DataProvider.isDisablePhoneCall();

                Logger.d(TAG, "onCallState:" + callMonitoring + " isCalling:" + isCalling + " disablePhoneCall:");
                saveLog("onCallState:" + callMonitoring + " isCalling:" + isCalling);
                saveLog("number:" + number + " contactName:" + contactName);
                Logger.d(TAG, "CALL_STATE_RINGING number:" + number + " contactName:" + contactName + " refusePhone:" + refusePhone);
                saveLog("refusePhone: " + refusePhone);

                if ((TextUtils.isEmpty(contactName) && refusePhone) ) {
                    return;
                }
                final boolean inForbidPeriodNow = DataProvider.inForbidPeriodNow();
                saveLog("inForbidPeriodNow: " + inForbidPeriodNow + " id" + AndroidUtils.getDeviceId());
                Logger.d(TAG, "inForbidPeriodNow:" + inForbidPeriodNow);
                if ((inForbidPeriodNow && TextUtils.isEmpty(contactName)) || callMonitoring || isCalling) {
                    Utils.endCall(mContext);
                    return;
                }

                if (TextUtils.isEmpty(number)) {
                    return;
                }
                showActivity(mContext, number);
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                if (!CacheUtils.isCallMonitoring(mContext)) {
                    if(!Utils.isHeadsetPluggedIn(mContext)){
                        Utils.openSpeaker(mContext);
                    }
                }
                offHookTime = System.currentTimeMillis();
                Utils.sendCallStateBroadCast(mContext, state);
            default:
                break;
        }
    }

    /**
     * 显示来电Activity
     *
     * @param ctx
     * @param number
     */
    private void showActivity(Context ctx, String number) {
        Intent intent = new Intent(ctx, InCallUIActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(InCallUIActivity.CALL_TYPE, 2);
        intent.putExtra(InCallUIActivity.EXTRA_PHONE_NUM, number);
        ctx.startActivity(intent);
    }
}
